/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.entity.RequestQueue;
import com.thienloc.omap.entity.RequestQueueHist;
import com.thienloc.omap.jdbc.dao.TblSmsBroadcastingDao;
import com.thienloc.omap.jdbc.entity.TblSmsBroadcasting;
import com.thienloc.omap.session.RequestQueueFacade;
import com.thienloc.omap.session.RequestQueueHistFacade;
import com.thienloc.omap.util.JsfUtil;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class RequestQueueHistBean extends AbstractCrudBean {

    @EJB
    private RequestQueueHistFacade facade;
    @EJB
    private RequestQueueFacade requestQueueFacade;
    private RequestQueueHist selected;
    private List<RequestQueueHist> list;
    private List<TblSmsBroadcasting> listMt;
    private RequestQueueHist[] selectedList;
    private boolean searched = false;
    private String selectedMsisdn;
    private Date startDate;
    private Date endDate;

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public RequestQueueHist[] getSelectedList() {
        return selectedList;
    }

    public void setSelectedList(RequestQueueHist[] selectedList) {
        this.selectedList = selectedList;
    }

    public List<RequestQueueHist> getList() {
        return list;
    }

    public void setList(List<RequestQueueHist> list) {
        this.list = list;
    }

    public RequestQueueHist getSelected() {
        return selected;
    }

    public void setSelected(RequestQueueHist selected) {
        this.selected = selected;
    }

    public String getSelectedMsisdn() {
        return selectedMsisdn;
    }

    public void setSelectedMsisdn(String selectedMsisdn) {
        this.selectedMsisdn = selectedMsisdn;
    }

    @Override
    protected void validateForm() throws Exception {
    }

    @Override
    protected Logger getLogger() {
        return LoggerFactory.getLogger(RequestQueueHistBean.class);
    }

    @Override
    protected void resetToCreateFormAction() throws Exception {
        selected = new RequestQueueHist();
        edit = false;
        view = false;
    }

    @Override
    protected void findSelected(int id) throws Exception {
        selected = facade.find(id);
    }

    @Override
    protected void createSelected() throws Exception {
        facade.create(selected);
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã thêm mới thành công một bản ghi");
    }

    @Override
    protected void editSelected() throws Exception {
        facade.edit(selected);
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã cập nhật thành công một bản ghi");
    }

    @Override
    protected void removeSelected() throws Exception {
        int size = selectedList.length;
        if (size == 0) {
            JsfUtil.addMessage(FacesMessage.SEVERITY_WARN, "Không có gì để xóa", "Hãy click chọn các bản ghi cần xóa");
            return;
        }
        for (RequestQueueHist item : selectedList) {
            facade.remove(item);
        }
        resetToCreateForm();
        JsfUtil.addSuccessMessage("Thành công!", "Đã xóa thành công " + size + " bản ghi");
    }

    @Override
    protected void prepareViewAction() throws Exception {
        view = true;
        edit = false;
        if (selectedList == null || selectedList.length == 0) {
            JsfUtil.addErrorMessage("Lỗi", "Bạn chưa tích chọn bản ghi cần hiển thị");
            return;
        }
        selected = selectedList[0];
        if (selectedList.length > 1) {
            JsfUtil.addMessage(FacesMessage.SEVERITY_WARN, "Cảnh báo hiển thị", "Bạn đã chọn nhiều hơn 1 bản ghi. Chỉ bản ghi trên cùng được hiển thị");
        }
    }

    @Override
    protected void prepareEditAction() throws Exception {
        view = false;
        edit = true;
        if (selectedList == null || selectedList.length == 0) {
            JsfUtil.addErrorMessage("Lỗi", "Bạn chưa tích chọn bản ghi cần hiển thị");
            return;
        }
        selected = selectedList[0];
        if (selectedList.length > 1) {
            JsfUtil.addMessage(FacesMessage.SEVERITY_WARN, "Cảnh báo hiển thị", "Bạn đã chọn nhiều hơn 1 bản ghi. Chỉ bản ghi trên cùng được hiển thị");
        }
    }

    public void searchMo() {
        list = new ArrayList<RequestQueueHist>();
        List<RequestQueue> requestQueues = requestQueueFacade.findByMsisdnInDateRange(selectedMsisdn, startDate, endDate);
        for (RequestQueue requestQueue : requestQueues) {
            RequestQueueHist requestQueueHist = new RequestQueueHist();
            requestQueueHist.setCallBack(requestQueue.getCallBack());
            requestQueueHist.setCpNumber(requestQueue.getCpNumber());
            requestQueueHist.setDeliverProvider(requestQueue.getDeliverProvider());
            requestQueueHist.setLastCheckTime(requestQueue.getLastCheckTime());
            requestQueueHist.setMsisdn(requestQueue.getMsisdn());
            requestQueueHist.setOldContent(requestQueue.getOldContent());
            requestQueueHist.setParam(requestQueue.getParam());
            requestQueueHist.setReceivedTime(requestQueue.getReceivedTime());
            requestQueueHist.setRequestDetail(requestQueue.getRequestDetail());
            requestQueueHist.setRequestId(new BigDecimal(requestQueue.getRequestId()));
            requestQueueHist.setResponseContent(requestQueue.getResponseContent());
            requestQueueHist.setSelected(requestQueue.getSelected());
            requestQueueHist.setServiceName(requestQueue.getServiceName());
            requestQueueHist.setStatus(requestQueue.getStatus());

            list.add(requestQueueHist);
        }

        list.addAll(facade.findByMsisdnInDateRange(selectedMsisdn, startDate, endDate));
        searched = true;
    }

    public void searchMt() {
        try {
            listMt = new TblSmsBroadcastingDao().findByMsisdn(selectedMsisdn, startDate, endDate);
            searched = true;
        } catch (Exception ex) {
            JsfUtil.addErrorMessage("Lỗi", ex.getMessage());
        }
    }

    public void searchByMsisdn(String msisdn) {
        this.selectedMsisdn = msisdn;
        list = new ArrayList<RequestQueueHist>();
        List<RequestQueue> requestQueues = requestQueueFacade.findByMsisdn(msisdn);
        for (RequestQueue requestQueue : requestQueues) {
            RequestQueueHist requestQueueHist = new RequestQueueHist();
            requestQueueHist.setCallBack(requestQueue.getCallBack());
            requestQueueHist.setCpNumber(requestQueue.getCpNumber());
            requestQueueHist.setDeliverProvider(requestQueue.getDeliverProvider());
            requestQueueHist.setLastCheckTime(requestQueue.getLastCheckTime());
            requestQueueHist.setMsisdn(requestQueue.getMsisdn());
            requestQueueHist.setOldContent(requestQueue.getOldContent());
            requestQueueHist.setParam(requestQueue.getParam());
            requestQueueHist.setReceivedTime(requestQueue.getReceivedTime());
            requestQueueHist.setRequestDetail(requestQueue.getRequestDetail());
            requestQueueHist.setRequestId(new BigDecimal(requestQueue.getRequestId()));
            requestQueueHist.setResponseContent(requestQueue.getResponseContent());
            requestQueueHist.setSelected(requestQueue.getSelected());
            requestQueueHist.setServiceName(requestQueue.getServiceName());
            requestQueueHist.setStatus(requestQueue.getStatus());

            list.add(requestQueueHist);
        }

        list.addAll(facade.findByMsisdn(msisdn));
//        Calendar threeMonthsAgo = Calendar.getInstance();
//        threeMonthsAgo.add(Calendar.MONTH, -3);
//        list.addAll(facade.findByMsisdnBeforeStartDate(msisdn, threeMonthsAgo.getTime()));
    }

    public List<TblSmsBroadcasting> getListMt() {
        return listMt;
    }

    public void reload() {
        if (searched) {
            searchMo();
        } else {
            resetToCreateForm();
        }
    }
}
